
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.*;

public class Appointments extends JFrame
{
    private JPanel southPanel = new JPanel();
    List<String> _appointments;
    private JList list;

    public Appointments(List<String> appointments)
    {
        _appointments = appointments;
        InitList();
        JButton btnAdd = new JButton("Add");
        btnAdd.addActionListener(
                new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        String temp = JOptionPane.showInputDialog("Enter appointment details:");
                        if(temp != null)
                            _appointments.add(temp);
                        InitList();
                    }
                });
        southPanel.add(btnAdd);
        JButton btnClear = new JButton("Clear");
        btnClear.addActionListener(
                new ActionListener()
                {
                        @Override
                        public void actionPerformed(ActionEvent e)
                        {
                            _appointments.clear();
                            InitList();
                        }
                });
        southPanel.add(btnClear);
        add(southPanel,BorderLayout.SOUTH);
    }

    private void InitList()
    {
        if(list!= null)
            remove(list);
        DefaultListModel model = new DefaultListModel();
        int i = 0;
        for(String s: _appointments)
        {
            model.add(i++, s);
        }
        list = new JList(model);
        add(list,BorderLayout.CENTER);
        revalidate();
        repaint();
    }

    public String[] getAppointments()
    {
        return _appointments.toArray(new String[0]);
    }
}

